home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / SocketIS.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  2.1 KB  |  95 lines

  1. package symantec.itools.db.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import symjava.sql.SQLException;
  6.  
  7. public final class SocketIS extends InputStream {
  8.    private byte[] data;
  9.    private int length;
  10.    private int offset;
  11.    InputStream _istrm;
  12.    byte[] _header;
  13.    int _msgnum;
  14.    boolean bClosed = false;
  15.    MPlex _mplex;
  16.  
  17.    public SocketIS(InputStream istrm, MPlex mplex) throws SQLException {
  18.       this._mplex = mplex;
  19.       this.length = 0;
  20.       this.offset = 0;
  21.       this._istrm = istrm;
  22.       this._header = new byte[5];
  23.    }
  24.  
  25.    public void close() throws IOException {
  26.       this._istrm.close();
  27.       this.bClosed = true;
  28.    }
  29.  
  30.    public int read() throws IOException {
  31.       if (this.offset >= this.length) {
  32.          try {
  33.             this.data = this.getData();
  34.             this.length = this.data.length;
  35.             if (this.length == 0) {
  36.                this.close();
  37.                return -1;
  38.             }
  39.  
  40.             this.offset = 0;
  41.          } catch (SQLException e) {
  42.             throw new IOException(((Throwable)e).getMessage());
  43.          }
  44.       }
  45.  
  46.       return this.data[this.offset++] & 255;
  47.    }
  48.  
  49.    byte[] getData() throws SQLException {
  50.       short streamID = (short)32767;
  51.  
  52.       try {
  53.          int lenRecv = 0;
  54.  
  55.          for(int npass = 0; lenRecv < 5 && npass < 4; ++npass) {
  56.             lenRecv += this._istrm.read(this._header, lenRecv, 5 - lenRecv);
  57.          }
  58.  
  59.          if (this._header[0] != -69) {
  60.             throw new SQLException("Ivalid Packet header");
  61.          } else {
  62.             int msglen = this._header[2];
  63.             if (msglen < 0) {
  64.                msglen += 256;
  65.             }
  66.  
  67.             msglen += (this._header[1] << 8) - 2;
  68.             streamID = (short)this._header[4];
  69.             if (streamID < 0) {
  70.                streamID += 256;
  71.             }
  72.  
  73.             streamID = (short)(streamID + (this._header[3] << 8));
  74.             int ipos = 0;
  75.  
  76.             byte[] msgbufRecv;
  77.             for(msgbufRecv = new byte[msglen]; msglen > 0; ipos += lenRecv) {
  78.                lenRecv = this._istrm.read(msgbufRecv, ipos, msglen);
  79.                msglen -= lenRecv;
  80.             }
  81.  
  82.             return msgbufRecv;
  83.          }
  84.       } catch (IOException var7) {
  85.          this._mplex.connectionLost();
  86.          throw new SQLServerConnException();
  87.       }
  88.    }
  89.  
  90.    public void reset() {
  91.       this.length = 0;
  92.       this.offset = 0;
  93.    }
  94. }
  95.